package com.yixing.demo.controller;

import com.yixing.demo.constants.Constants;
import com.yixing.demo.model.Friend;
import com.yixing.demo.model.MessageSession;
import com.yixing.demo.model.MessageSessionUser;
import com.yixing.demo.model.User;
import com.yixing.demo.service.MessageService;
import com.yixing.demo.service.MessageSessionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Slf4j
@RequestMapping("/messagesession")
@RestController
public class MessageSessionController {
    @Resource
    MessageSessionService messageSessionService;

    @Resource
    MessageService messageService;
    @RequestMapping("/getsessionList")
    public Object getsessionList(HttpServletRequest req) {
        List<MessageSession> messageSessionList = new ArrayList<>();
        HttpSession session = req.getSession(false);
        if(session == null) {
            log.info("获取不到session对象");
            return messageSessionList;
        }
        User userInfo = (User)session.getAttribute(Constants.USER_SESSION);
        if(userInfo == null) {
            log.info("在session中获取不到user对象");
            return messageSessionList;
        }
        List<Integer> sessionIds = messageSessionService.getSessionIdsByUserId(userInfo.getUserId());
        for (int sessionId : sessionIds) {
            MessageSession messageSession = new MessageSession();
            messageSession.setSessionId(sessionId);
            List<Friend> friends = messageSessionService.getFriendsBySessionId(sessionId, userInfo.getUserId());
            messageSession.setFriends(friends);
            String content = messageService.getLastMessageBySessionId(sessionId);
            if(content == null) {
                messageSession.setLastMessage("");
            }else {
                messageSession.setLastMessage(content);
            }
            messageSessionList.add(messageSession);
        }
        return messageSessionList;
    }


    @RequestMapping("/addsession")
    @Transactional
    public Object addsession(int toFriendId, @SessionAttribute(Constants.USER_SESSION) User user) {
        log.info("已进入后端");
        HashMap<String, Integer> hashMap = new HashMap<>();
         MessageSession messageSession = new MessageSession();
         messageSessionService.addMessageSession(messageSession);

        MessageSessionUser messageSessionUser1 = new MessageSessionUser();
        messageSessionUser1.setSessionId(messageSession.getSessionId());
        messageSessionUser1.setUserId(user.getUserId());
        messageSessionService.addMessageSessionUser(messageSessionUser1);

        MessageSessionUser messageSessionUser2 = new MessageSessionUser();
        messageSessionUser2.setSessionId(messageSession.getSessionId());
        messageSessionUser2.setUserId(toFriendId);
        messageSessionService.addMessageSessionUser(messageSessionUser2);

        hashMap.put("sessionId", messageSession.getSessionId());
        return hashMap;
    }
}
